<?php

class DataService_Model {

  /**
   * check whether or not support object base type
   * @var boolean
   */
  protected $_inherit = FALSE;
  /**
   *
   * @var string
   */
  protected $_pk = 'id';
  /**
   *
   * @var string
   */
  protected $_tbl;
  /**
   *
   * @var array
   */
  protected $_data;

  public function __construct($data = NULL) {
    if ($data) {
      $this->_data = $data;
    }
  }

  /**
   *
   * @param  string $name
   * @return mixed
   */
  public function __get($name) {
    $method_name = "get" . ucfirst($name);
    if (method_exists($this, $method_name)) {
      return $this->{$method_name}();
    }
    return @$this->_data[$name];
  }

  /**
   *
   * @param string $name
   * @param mixed $value
   */
  public function __set($name, $value) {
    $method_name = "set" . ucfirst($name);
    if (method_exists($this, $method_name)) {
      return $this->{$method_name}($value);
    }
    return $this->_data[$name] = $value;
  }

  /**
   * magic method
   * ignore clone object
   * @return false
   */
  public function __clone() {
    
  }

  public function findById($id) {
    $db = Db::factory();
    return new User_Model_User($db->fetchRow('select * from user_account where '.$this->_pk.' = :id', array('id' => $id)));
  }

  /**
   * public function insert
   */
  public function insert() {

  }

  public function getId(){
    return $this->_data[$this->_pk];
  }

  public function setId($value){
    return $this->_data[$this->_pk] =  $value;
  }
  /**
   * call update function
   */
  public function update() {
    $keylist = array();
    foreach ($this->_data as $key => $value) {
      $keylist[] = $key.'=  :'.$key;
    }
    
    
    $sql = "update $this->_tbl set " . implode(',', $keylist) . " where $this->_pk = :$this->_pk";
    return Db::factory()->query($sql, $this->_data);
  }

  /**
   *
   * @return $data delete
   */
  public function save($data = NULL) {
    return $this->_data;
  }

  /**
   * delete user instance
   */
  public function delete() {
    
  }

}